<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 退货/拒收ajax类
 * @package  ajax
 * @subpackage  backend
 * @author   create by allen.qiang
 */

quanli_require('module/base/bill/billReturn.class.php');

class ajax_aoliBillReturn {

    public  function execute () {

        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch ($_REQUEST['operFlg']){

                case 1:     //退货单查询
                    $this->billReturnList();
                    break;

                case 101:     //退货单取消
                    $this->cancelBillReturn();
                    break;

                case 5:
                    $this->searchGoodsDetail();
                    break;

                case 6:
                    $this->searchGoodsList();
                    break;

                case 7:
                    $this->billReturn();
                    break;

                default:
                    returnJsonData('参数错误');
                    break;

            }

        } else {

            returnJsonData('参数错误');

        }

    }

    private function billReturn() {

        if (!isset($_REQUEST['return_no']) || !is_numeric($_REQUEST['return_no'])) {
            returnJsonData('缺少退货单号');
        }

        global $G_PGLOB;

        //实例订单退换货类
        $billReturn = new billReturn( $G_PGLOB->DBCA );

        $arg_array = array(
            'return_no' => $_REQUEST['return_no']
        );

        //退货单主表信息
        $bill_return_array = $billReturn->searchBillReturn( $arg_array );
        if (!$bill_return_array) {
            returnJsonData('退货单信息没有找到', 404);
        }
        $bill_return_array = $bill_return_array[0];

        //退货商品信息
        $bill_return_array['return_goods'] = $billReturn->searchBillReturnGoods( $arg_array );
        if (!$bill_return_array['return_goods']) {
            returnJsonData('退货商品信息没有找到', 404);
        }

        //退货支付方式
        $bill_return_pay_array = $billReturn->searchBillReturnPay( $arg_array );
        if($bill_return_pay_array){
            foreach($bill_return_pay_array as $key => $return_pay){
                $bill_return_array['pay'][$key]['payTypeName'] = $return_pay['payTypeName'];
                $bill_return_array['pay'][$key]['return_pay'] = $return_pay['return_pay'];
            }
        }

        //退货支付信息
        $sql = "SELECT * FROM bill_return_coupon_card WHERE return_no = ".$_REQUEST['return_no'];
        $return_array = $G_PGLOB->DBCA->stmt_query($sql);

        if($return_array){
            foreach($return_array as $key => $return){
                $bill_return_array['coupon'][$key]['coupon_code'] = $return->coupon_code;
                $bill_return_array['coupon'][$key]['coupon_name'] = $return->coupon_name;
                $bill_return_array['coupon'][$key]['coupon_money'] = $return->coupon_money;
                $bill_return_array['card'][$key]['card_no'] = $return->card_no;
                $bill_return_array['card'][$key]['card_name'] = $return->card_name;
                $bill_return_array['card'][$key]['card_money'] = $return->card_money;
            }
        }

        returnJsonData('ok', 200, $bill_return_array);

    }

    /**
     * 查询退货列表
     */
    private function billReturnList() {

        global $G_PGLOB;

        //查询条件选择了配送站 则根据选择的配送站查询
        if(!isset( $_REQUEST['distribution_id']) || !is_numeric($_REQUEST['distribution_id']) ){
            $_REQUEST['deliver_cityid'] = $_REQUEST['deliver_cityid'];

            $PS_LOGIN_SESSION =$G_PGLOB->sessionCache_get('PS_LOGIN_SESSION');

            $distribution_id_array = $PS_LOGIN_SESSION['distribution'];//权限下的配送站id数组

            if( count($distribution_id_array) > 1){

                $distribution_id_arg = implode( ',',$distribution_id_array );

                $_REQUEST['extend'] = ' AND b.distribution_id IN ('.$distribution_id_arg.')';

            }else{
                $_REQUEST['distribution_id'] = $distribution_id_array[0];

            }

            //查询条件没有没有选择配送站 则根据当前登录用户权限下的配送站查询
        }else{

            global $G_PGLOB;

            $PS_LOGIN_SESSION =$G_PGLOB->sessionCache_get('PS_LOGIN_SESSION');

            $distribution_id_array = $PS_LOGIN_SESSION['distribution'];//权限下的配送站id数组

            if( count($distribution_id_array) > 1){

                $distribution_id_arg = implode( ',',$distribution_id_array );

                $_REQUEST['extend'] = ' AND b.distribution_id IN ('.$distribution_id_arg.')';

            }else{
                $_REQUEST['distribution_id'] = $distribution_id_array[0];

            }
        }

        //实例订单退换货类
        $billReturn = new billReturn( $G_PGLOB->DBCA );

        $_REQUEST['billPlatform'] = 2;

        $bill_info_array = $billReturn->searchBillReturn( $_REQUEST );

        //组织返回的数据
        $result_array = array(
            'page' => $_REQUEST['page'],
            'total' => $_REQUEST['count'],
            'rows' => array()
        );

        if ($bill_info_array) {
            foreach($bill_info_array as &$bill) {

                $operate = '';
                if ($bill['return_status'] == 1) {

//                    if($G_PGLOB->has_permission(1007)){
                        $operate .= '<a href="javascript: void(0);" onclick="bill.view.open(' . $bill['return_no'] . ', 4);">取消</a>';
//                    }

                }

                $return_reason = $bill['return_reason_1'];
                if ($bill['return_reason_2']) {
                    $return_reason .= '_' . $bill['return_reason_2'];
                }

                $result_array['rows'][] = array(
                    'id' => $bill['return_no'],
                    'cell' => array(
                        $operate,
                        $bill['return_status_name'],
                        '<a href="javascript: void(0);" onclick="bill.view.open(' . $bill['return_no'] . ', 0);">' . $bill['return_no'] . '</a>',
                        '<a href="javascript: void(0);" onclick="bill.billview(\''.$bill['bill_no'].'\');">'.$bill['bill_code'].'</a>',
                        //add by allen.qiang 20141024 退货单增加“配送日期”作为查询条件--start
                        $bill['send_Date'],
                        //add by allen.qiang 20141024 退货单增加“配送日期”作为查询条件--end
                        $bill['custID'],
                        $bill['custName'],
                        $bill['mobile'],
                        $return_reason,
                        $bill['distribution_name'],
                        $bill['creator'],
                        $bill['createTime'],
                        $bill['return_memo'],
                        $bill['confirmor'],
                        $bill['confirm_time'],
                        $bill['delivery_operator'],
                        $bill['delivery_time'],
                        $bill['cancel_operator'],
                        $bill['cancel_time']
                    )
                );
            }
        }

        echo json_encode($result_array);

        return;
    }

    /**
     * 退换货单取消
     */
    private function cancelBillReturn() {

        if (!isset($_REQUEST['return_no']) || !is_numeric($_REQUEST['return_no'])) {
            returnJsonData('参数错误');
        }

        global $G_PGLOB;

        //实例订单退换货类
        $billReturn = new billReturn( $G_PGLOB->DBCA );

        $arg_array = array(
            'type' => 4,
            'return_no' => $_REQUEST['return_no'],
            'operator' => $G_PGLOB->currentUserName,
            'datetime' => $G_PGLOB->sys_datetime
        );

        $result_array = $billReturn->cancel( $arg_array );

        returnJsonData( $result_array['message'], $result_array['code'] );

    }

    /**
     * 查询三级商品信息
     */
    private function searchGoodsDetail() {

        //检查商品编号
        if (!isset($_REQUEST['product_code']) || empty($_REQUEST['product_code'])) {
            returnJsonData('商品编号为空');
        }

        global $G_SHOP;

        //加载类文件
        quanli_require('module/base/goods/quanliGoods.class.php');

        //实例商品类
        $quanliGoods = new quanliGoods( $G_SHOP->DBCA );

        //生成商品编号数组
        $product_code_array = explode(',', $_REQUEST['product_code']);

        $return_goods_array = array();

        //循环商品数组，查询商品信息
        foreach($product_code_array as &$product_code) {

            //查询商品信息
            $arg_array = array(
                'product_code' => $product_code,
                'useFlg' => 1,
                'forsale' => 1
            );
            $goods_array = $quanliGoods->searchGoodsSale( $arg_array );
            if (!$goods_array) {
                returnJsonData('商品没有找到或已下架', 404);
            }

            $return_goods_array[] = $goods_array;

        }

        returnJsonData('ok', 200, $return_goods_array);

    }

    /**
     * 查询二级商品
     */
    private function searchGoodsList() {

        //检查商品编号和商品名称
        if (!isset($_REQUEST['product_code'], $_REQUEST['goods_name']) || (empty($_REQUEST['product_code']) && empty($_REQUEST['goods_name']))) {
            returnJsonData('参数错误');
        }

        global $G_SHOP;

        //加载类文件
        quanli_require('module/base/goods/quanliGoods.class.php');

        $product_code = $_REQUEST['product_code'];
        $goods_name = $_REQUEST['goods_name'];

        //实例商品类
        $quanliGoods = new quanliGoods( $G_SHOP->DBCA );

        //查询二级商品信息
        $arg_array = array(
            'product_code' => $product_code,
            'goodsName' => $goods_name,
            'useFlg' => 1,
            'forsale' => 1
        );
        $goods_list_array = $quanliGoods->searchGoodsList( $arg_array );
        if (!$goods_list_array) {
            returnJsonData('商品没有找到或已下架', 404);
        }

        $goods_array = array();

        foreach($goods_list_array as &$goods_list) {

            //查询三级商品
            $arg_array = array(
                'twoPostID' => $goods_list['postID'],
                'useFlg' => 1,
                'forsale' => 1
            );
            $goods_sale_array = $quanliGoods->searchGoodsSale( $arg_array );
            if (!$goods_sale_array) {
                continue;
            }

            //组装三级商品信息
            $tmp_sale_array = array();
            foreach($goods_sale_array as &$goods_sale) {
                $tmp_sale_array[] = array(
                    'postID' => $goods_sale['postID'],
                    'product_code' => $goods_sale['product_code'],
                    'goodsName' => $goods_sale['goodsName'],
                    'spec' => $goods_sale['spec']
                );
            }

            //组装返回的商品信息
            $goods_array[] = array(
                'postID' => $goods_list['postID'],
                'goodsName' => $goods_list['goodsName'],
                'spec' => $goods_list['spec'],
                'list' => $tmp_sale_array
            );

        }

        if (empty($goods_array)) {
            returnJsonData('商品没有找到或已下架', 404);
        }

        returnJsonData('ok', 200, $goods_array);

    }


}